package com.imau.controller;

import com.imau.bean.User;
import com.imau.service.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/login")
public class UserController extends HttpServlet{


    private UserServiceImpl userService = new UserServiceImpl();

    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType(("text/html;charset=utf-8"));

        String username = req.getParameter("username");
        String password = req.getParameter("password");

        User user=userService.checkLogin(username,password);
        if (user!=null){
            resp.sendRedirect(req.getContextPath()+"/studentList-Element.html");
        }else{
            resp.sendRedirect(req.getContextPath()+"/login.html");
        }
    }
}


